package com.ywjh.inter.service;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

import com.ywjh.inter.conn.DBConnection;
import com.ywjh.inter.dao.DietDAO;
import com.ywjh.inter.dao.DietFileDAO;
import com.ywjh.inter.dao.DietPatientDAO;
import com.ywjh.inter.dao.DietTempDAO;
import com.ywjh.inter.entity.DietEntity;
import com.ywjh.inter.entity.DietFileEntity;
import com.ywjh.inter.entity.DietPatientEntity;
import com.ywjh.inter.entity.DietTempEntity;

public class DietService {
	private static DietService instance;
	private DietDAO dietDAO;
	private DietTempDAO dietTempDAO;
	private DietFileDAO dietFileDAO;
	private DietPatientDAO dietPatientDAO;

	public static DietService getInstance() {
		if (instance == null) {
			instance = new DietService();
			instance.dietDAO = new DietDAO();
			instance.dietTempDAO = new DietTempDAO();
			instance.dietPatientDAO = new DietPatientDAO();
			instance.dietFileDAO = new DietFileDAO();
		}
		return instance;
	}

	public boolean addDiet(DietEntity dietEntity) {
		boolean b = false;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			b = dietDAO.insert(conn, dietEntity);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}

		return b;
	}

	public boolean addDietTemplate(DietTempEntity dietEntity) {
		boolean b = false;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			b = dietTempDAO.insert(conn, dietEntity);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}

		return b;
	}

	public List<DietTempEntity> listTemplateByDocId(int doc_id) {
		List<DietTempEntity> list = null;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			list = dietTempDAO.listByDocId(conn, doc_id);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return list;
	}

	public List<DietEntity> listByPatientId(int patient_id, int doc_id) {
		List<DietEntity> list = null;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			list = dietDAO.listByPatientId(conn, patient_id, doc_id);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return list;
	}

	public List<DietEntity> listByPatientId(int patient_id) {
		List<DietEntity> list = null;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			list = dietDAO.listByPatientId(conn, patient_id);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return list;
	}

	public List<DietPatientEntity> listDiePatientByDietId(int diet_id, int patient_id) {
		List<DietPatientEntity> list = null;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			if (diet_id == 0) {
				list = dietPatientDAO.listByPatientId(conn, patient_id);
			} else {
				list = dietPatientDAO.listByDietId(conn, diet_id, patient_id);
			}
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return list;
	}

	public List<DietPatientEntity> listDiePatientByDietId(int diet_id) {
		List<DietPatientEntity> list = null;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			list = dietPatientDAO.listByDietId(conn, diet_id);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return list;
	}

	public List<DietFileEntity> findFile(int id) {
		Connection conn = null;
		List<DietFileEntity> list = null;
		try {
			conn = DBConnection.getDBConnection();
			list = dietFileDAO.find(conn, id);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return list;
	}

	public int addPatientDiet(DietPatientEntity dietPatientEntity) {
		int id = -1;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			id = dietPatientDAO.insert(conn, dietPatientEntity);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return id;
	}

	public boolean updatePatientDiet(DietPatientEntity dietPatientEntity) {
		boolean b = false;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			b = dietPatientDAO.update(conn, dietPatientEntity);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return b;
	}

	public DietPatientEntity findPatientDietById(int id) {
		Connection conn = null;
		DietPatientEntity entity = null;
		try {
			conn = DBConnection.getDBConnection();
			entity = dietPatientDAO.findById(conn, id);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return entity;
	}

	public boolean saveDietFile(int diet_patient_id, String[] filenames, String[] types) {
		Connection connection = null;
		boolean b = false;
		try {
			connection = DBConnection.getDBConnection();
			connection.setAutoCommit(false);
			dietFileDAO.deleteBydpid(connection, diet_patient_id);
			int size = filenames.length;
			for (int i = 0; i < size; i++) {
				DietFileEntity dietFileEntity = new DietFileEntity();
				dietFileEntity.setDiet_patient_id(diet_patient_id);
				dietFileEntity.setFilename(filenames[i]);
				dietFileEntity.setType(types[i]);
				dietFileDAO.insert(connection, dietFileEntity);
			}
			connection.commit();
			b = true;
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
			try {
				connection.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				connection.setAutoCommit(true);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			DBConnection.close(connection);
		}
		return b;
	}

	public List<DietFileEntity> listDietFileBydpId(int dpid) {
		List<DietFileEntity> list = null;
		Connection connection = null;
		try {
			connection = DBConnection.getDBConnection();
			list = dietFileDAO.listBydpid(connection, dpid);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(connection);
		}
		return list;
	}

	public DietPatientEntity findPatientDietByTime(Timestamp timestamp, int patient_id, String type) {
		DietPatientEntity entity = null;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			entity = dietPatientDAO.findByTime(conn, timestamp, patient_id, type);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return entity;
	}

	public void disableDieteByPatient(int patient_id) {
		Connection connection = null;
		try {
			connection = DBConnection.getDBConnection();
			dietDAO.disableByPatientID(connection, patient_id);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(connection);
		}
	}

}
